//
//  MTProgressHUD.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/2.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import Foundation
import UIKit
class MTProgressHUD {
    
    class func show(to:UIView,animated:Bool) {
        
        let HUD = MBProgressHUD.showAdded(to:to, animated: animated)
        
        HUD.bezelView.color = UIColor.clear
        HUD.backgroundView.style = .solidColor
        
    }
    
    class func show() {
        show(to:UIApplication.shared.keyWindow!,animated:true)
    }
    
    class func show(to:UIView,animated:Bool,string:String) {
        
        let HUD = MBProgressHUD.showAdded(to:to, animated: animated)
        
        HUD.bezelView.color = UIColor.clear
        HUD.backgroundView.style = .solidColor
        HUD.label.text = string
        
    }
    
    class func updateProgressDesc(_ view:UIView ,desc:String) {
        let HUD:MBProgressHUD? = MBProgressHUD(for:view)
        if HUD != nil{
            HUD?.label.text = desc
        }
        
    }
    
    class func show(_ string:String) {
        
        show(to: UIApplication.shared.keyWindow!, animated: true, string: string)
    }
    
    class func show(to:UIView,animated:Bool,string:String,details:String) {
        
        let HUD = MBProgressHUD.showAdded(to:to, animated: animated)
        
        HUD.bezelView.color = UIColor.clear
        HUD.backgroundView.style = .solidColor
        HUD.label.text = string
        HUD.detailsLabel.text = details
    }
    
    
    class func show(to:UIView,animated:Bool,delay:TimeInterval) {
        
        let HUD = MBProgressHUD.showAdded(to:to, animated: animated)
        
        HUD.bezelView.color = UIColor.clear
        HUD.backgroundView.style = .solidColor
        
        HUD.hide(animated:true, afterDelay: delay)
    }
    
    class func showTips(to:UIView,string:String,delay:TimeInterval) {
        MBProgressHUD.hide(for: to, animated: true)
        let HUD = MBProgressHUD.showAdded(to:to, animated: true)
        
        HUD.bezelView.color = UIColor.clear
        HUD.label.text = string
        let with = min(VIEW_WIDTH(view:to) - 30 , CGFloat(string.characters.count*17))
        
        let height = min(string.stringHeightWith(fontSize:16,width:with), VIEW_HEIGHT(view: to) * 0.8) + 20
        
        let label:UILabel = UILabel.init(frame: CGRect.init(x: (VIEW_WIDTH(view: to) - with)/2, y: (VIEW_HEIGHT(view: to) - height)/2, width:with , height: height))
        HUD.bezelView.isHidden = true
        
        label.backgroundColor = RGBA(r: 0, g: 0, b: 0, a: 0.8)
        label.layer.cornerRadius = 3
        label.clipsToBounds = true
        label.text = string
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.white
        label.textAlignment = .center
        label.numberOfLines = 0
        
        HUD.addSubview(label)
        HUD.hide(animated:true, afterDelay: delay)
        HUD.isHidden = false
        HUD.isUserInteractionEnabled = false
    }
    
    class func showTips(string:String,delay:TimeInterval){
        showTips(to: UIApplication.shared.keyWindow!, string: string, delay: delay)
    }
    
    class func hidded(to:UIView,animated:Bool)  {
        MBProgressHUD.hide(for: to, animated: animated)
    }
    class func hidded(_ animated:Bool)  {
        MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: animated)
    }
}
